<template>
	<view class="page">
		<!-- 订单tab -->
		<view class="order-tab">
			<view class="tab" :class="{'action': OrderType === 2}" @click="onOrderTab(2)">
				<text>待确认</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action': OrderType == 1}" @click="onOrderTab(1)">
				<text>交换中</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action': OrderType == 3}" @click="onOrderTab(3)">
				<text>已完成</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action': OrderType == 4}" @click="onOrderTab(4)">
				<text>已拒绝</text>
				<text class="line"></text>
			</view>
		</view>
		<!-- 订单列表 -->
		<view class="order" v-for="(el, idx) in goodsList" :key="idx">
			<view class="address">
				<text>对方联系信息</text>
				<view class="title">
					姓名:{{el.address.name}}
				</view>
				<view class="title">
					电话:{{el.address.mobile}}
				</view>
				<view class="title">
					地址:{{el.address.address}}
				</view>
			</view>
			<view class="orderNumber">
				<view class="number">
					{{el.order_sn}}
				</view>
				<view class="date">
					{{el.ctime}}
				</view>
				<view class="ischange" v-if="OrderType == 2">
					待确认
				</view>
				<view class="ischange" v-if="OrderType == 1">
					交换中
				</view>
				<view class="ischange" v-if="OrderType == 3">
					已完成
				</view>
				<view class="ischange" v-if="OrderType == 4">
					已拒绝
				</view>
			</view>
			<view class="product">
				<view class="name"  @click="gotogos(el)">
					<image :src="el.selfgoods.img_list[0]" mode=""></image>
					<text>{{el.selfgoods.title}}</text>
				</view>
				<view class="">
					交易由对方发起
				</view>
				<view class="name" @click="gotogos(el)">
					<image :src="el.togoods.img_list[0]" mode=""></image>
					<text>{{el.togoods.title}}</text>
				</view>
			</view>
			<!-- 根据订单类型显示不同的按钮 -->
			<view class="button" v-if="OrderType == 4">
				<text>{{el.reject_msg}}</text>
				
			</view>
			<view class="button" v-else>
				<view v-if="OrderType == 2" class="button1" @click="onReject(el)">
					拒绝
				</view>
				<view v-if="OrderType == 2" class="button1" @click="onAgree(el)">
					同意
				</view>
				<!-- <view v-if="OrderType == 2" class="button1" @click="onConfirmReceipt">
					确认收货
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getyihuoorder,
		yihuoorder
	} from '@/config/api.js';
	export default {
		data() {
			return {
				queryList: {
					isStore:1,
					page: 1,
					limit: 20,
					nav_id: uni.getStorageSync('pathNavId'),
					store_id: uni.getStorageSync('pathStoreId'),
					status: 2,
				},
				OrderType: 2, // 设置默认状态为待确认
				goodsList: [],
			};
		},
		onLoad(params) {
			uni.setStorageSync('yihuogoods','')
			this.getOrderList();
		},
		methods: {
			gotogos(data){
				uni.setStorageSync('yihuogoods',data)
				uni.navigateTo({
					url: '/moduleC/details/details',
					animationType: 'zoom-fade-out',
					animationDuration: 200
				})
			},
			getOrderList(){
				getyihuoorder(this.queryList).then(res => {
					this.goodsList = res.data; // 获取订单列表
				});
			},
			/**
			 * 返回点击
			 */
			onBack() {
				uni.navigateBack();
			},
			/**
			 * 订单tab点击
			 */
			onOrderTab(type) {
				this.queryList.status = type;
				this.getOrderList();
				this.OrderType = type; // 设置当前类型
				// #ifdef H5
				uni.redirectTo({
					url: '/pages/MyOrderList/MyOrderList?type=' + type,
				});
				// #endif
			},
			/**
			 * 订单列表点击
			 */
			onOrderList(id) {
				uni.navigateTo({
					url: '/pages/OrderDetails/OrderDetails?oderId=' + id,
				});
			},
			/**
			 * 拒绝订单
			 */
			onReject(data) {
				// 处理拒绝逻辑
				console.log('拒绝订单');
				const _this = this;
				uni.showModal({
					title: '确认拒绝吗？',
					editable:true,
					placeholderText: '请输入理由',
					success: function(res) {
						console.log(res)
						if (res.confirm) {
							const sdata = {
								id: data.id,
								status: 4,
								reject_msg: res.content
							}
							yihuoorder(sdata).then(res => {
								 uni.$u.toast('操作成功!')
							});
						}
					}
				});
			},
			/**
			 * 同意订单
			 */
			onAgree(data) {
				const _this = this;
				uni.showModal({
					title: '确认同意吗？',
					success: function(res) {
						console.log(res)
						if (res.confirm) {
							const sdata = {
								id: data.id,
								status: 1,
							}
							yihuoorder(sdata).then(res => {
								 uni.$u.toast('操作成功!')
							});
						}
					}
				});
				// 处理同意逻辑
				console.log('同意订单');
			},
			/**
			 * 确认收货
			 */
			onConfirmReceipt() {
				// 处理确认收货逻辑
				console.log('确认收货');
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'exchangeOrder.scss';
</style>